
clc
clear

% Calculates the MTOW using Raymer method


%% Initialization

% Global variables

global onePersonWeightLb
global errorToleranceLb
globalVariables


%% Loading Engines from database

dbEngines;


%% Loading historiacal data

global emptyWeightHistoricalCoeffs
emptyWeightCoeffs;


%% Loading specs ranges

[specs] = xlsread('aircraft_range.xls');

% Reading aircraft files

specCrewMin = specs(1,1);
specCrewMax = specs(1,2);
specCrewInterval = specs(1,3);
specPaxMin = specs(2,1);
specPaxMax = specs(2,2);
specPaxInterval = specs(2,3);
specLoadMin = specs(3,1);
specLoadMax = specs(3,2);
specLoadInterval = specs(3,3);
specRangeMin = specs(4,1);
specRangeMax = specs(4,2);
specRangeInterval = specs(4,3);
specCruiseSpeedMin = specs(5,1);
specCruiseSpeedMax = specs(5,2);
specCruiseSpeedInterval = specs(5,3);

aircraft.structureMaterial = specs (6,1);
aircraft.structureMaterial = 'alum';
aircraft.ldMax = specs (7,1);
aircraft.ldCruise = specs (8,1);
aircraft.loiterDuration = specs (9,1);
aircraft.idEngine = specs(10,1);


%% Calculating MTOW

aircraft.nbCrew = specCrewMin;
aircraft.nbPax = specPaxMin;
aircraft.weights.load = specLoadMin;
aircraft.range = specRangeMin;
aircraft.cruiseSpeed = specCruiseSpeedMin;

% index of the aircraft evaluated
i = 1;

while aircraft.nbCrew <= specCrewMax

    while aircraft.nbPax <= specPaxMax

        while aircraft.weights.load <= specLoadMax

            while aircraft.range <= specRangeMax;

                while aircraft.cruiseSpeed <= specCruiseSpeedMax;

                    % mtow calculated and aircraft structure updated
                    aircrafts(i) = calcMTOWRaymer( aircraft, engines )
                    weights = aircrafts(i).weights

                    disp(strcat('MTOW calculated - aircraft : ', int2str(i)));

                    i = i + 1;
                    aircraft.cruiseSpeed = aircraft.cruiseSpeed + specCruiseSpeedInterval;
                end
                
                aircraft.cruiseSpeed = specCruiseSpeedMin;
                aircraft.range = aircraft.range + specRangeInterval;
            end
            
            aircraft.range = specRangeMin;
            aircraft.weights.load = aircraft.weights.load + specLoadInterval;
        end
        
        aircraft.weights.load = specLoadMin;
        aircraft.nbPax = aircraft.nbPax + specPaxInterval;
    end
    
    aircraft.nbPax = specPaxMin;
    aircraft.nbCrew = aircraft.nbCrew + specCrewInterval;
end % end of aircraft.nbCrew loop


%% Creating spreadsheet

generateMTOWRepport( aircrafts, engines );
